import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import SxsView from '../views/SxsView.vue'
import IndexView from '../views/IndexView.vue'
import SouView from '../views/SouView.vue'
import { bannerdengruxinxi } from '../api'
import store from '@/store'
import FenyiView from '../views/CMS/FenyiView.vue'
import FenerView from '../views/CMS/FenerView.vue'
import FensanView from '../views/CMS/FensanView.vue'
import FensiView from '../views/CMS/FensiView.vue'
import FenwuView from '../views/CMS/FenwuView.vue'
import BianjiView from '../views/CMS/BianjiView.vue'
import MeryiView from '../views/MER/MeryiView.vue'
import MererView from '../views/MER/MererView.vue'
import MersanView from '../views/MER/MersanView.vue'
Vue.use(VueRouter)
const routes = [{
        path: '/',
        name: 'home',
        component: HomeView,
        redirect: "/sxs"
    },
    {
        path: '/Sxs',
        name: 'sxs',
        component: SxsView,
    },
    {
        path: '/Index',
        name: 'index',
        component: IndexView,
        children: [
            {
                path: '/sou',
                name: 'sou',
                component: SouView,
            },
            {
                path: '/CMS/fenyi',
                name: 'fenyi',
                component: FenyiView,
            },
            {
                path: '/CMS/fener/',
                name: 'fener',
                component: FenerView,
            },
            {
                path: '/CMS/fensan',
                name: 'fensan',
                component: FensanView,
            },
            {
                path: '/CMS/fensi',
                name: 'fensi',
                component: FensiView,
            },
            {
                path: '/CMS/fenwu',
                name: 'fenwu',
                component: FenwuView,
            },
            {
                path: '/CMS/fenwu',
                name: 'fenwu',
                component: FenwuView,
            },
            {
                path: '/CMS/bianji',
                name: 'bianji',
                component: BianjiView,
            },
            {
                path: '/MER/meryi',
                name: 'meryi',
                component: MeryiView,
            },
            {
                path: '/MER/merer',
                name: 'merer',
                component: MererView,
            },
            {
                path: '/MER/mersan',
                name: 'mersan',
                component: MersanView,
            },
        ]
    },
    {
        path: '/about',
        name: 'about',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import( /* webpackChunkName: "about" */ '../views/AboutView.vue')
    }
]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    // to 去哪
    // from 从哪来
    // next 放行 是否能通过

    // 获取存储的信息
    let userInfo = localStorage.getItem("userInfo") ? JSON.parse(localStorage.getItem("userInfo")) : {}
    let token = userInfo.token //登录标识
    if (token) {
        // console.log('aaaaaa');
        bannerdengruxinxi().then(res => {
            if(res.code===0){
                store.commit("bannerdengruxinxi",res.data)
            }else{
                localStorage.removeItem('userInfo')
        next('/index')

            }
            // console.log(res);
            store.commit('yonghu', res.data);
        })
    }
    // 如果用户已经登录过了，还要去登录页，直接让它跳转到首页
    if (to.path == '/sxs') {
        next()
    } else {
        // 这属于登录表示
        console.log(token);
        if (token) {
            next()
            // 如果没有token,直接退回登录
        } else {
            next('/sxs')
        }
    }
})


export default router